'
' Will Strohl (wills@strohlsitedesign.com)
' http://www.willstrohl.com
'
'Copyright (c) 2009, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of StrohlSiteDesign.com nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Collections.Generic
Imports System.Reflection
Imports System.Text.RegularExpressions

Imports WillStrohl.Modules.FeedHandler.FeedHandlerController

Namespace WillStrohl.Modules.FeedHandler

    ''' <summary>
    ''' This is the view control for the FeedHandler module.
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    Partial Class EditRule
        Inherits WNSPortalModuleBase

#Region " Private Members "

        Private p_RuleId As Integer = Null.NullInteger
        Private p_Feed As FeedInfo = Nothing
        Private p_Rule As FeedRuleInfo = Nothing
        Private p_FeedId As Integer = Null.NullInteger

#End Region

#Region " Private Properties "

        ''' <summary>
        ''' RuleId - this property houses the RuleId from the querystring
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Private ReadOnly Property RuleId() As Integer
            Get
                If Me.p_RuleId = Null.NullInteger Then
                    If Not Request.Params("RuleId") Is Nothing Then
                        Me.p_RuleId = Integer.Parse(Request.Params("RuleId").ToString, Globalization.NumberStyles.Integer)
                    End If
                End If

                Return Me.p_RuleId
            End Get
        End Property

        ''' <summary>
        ''' FeedId - this property houses the FeedId from the querystring
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Private ReadOnly Property FeedId() As Integer
            Get
                If Me.p_FeedId = Null.NullInteger Then
                    If Not Request.Params("FeedId") Is Nothing Then
                        Me.p_FeedId = Integer.Parse(Request.Params("FeedId").ToString, Globalization.NumberStyles.Integer)
                    End If
                End If

                Return Me.p_FeedId
            End Get
        End Property

        ''' <summary>
        ''' Feed - this property houses the Feed object
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Private ReadOnly Property Feed() As FeedInfo
            Get
                If Me.p_Feed Is Nothing Then
                    Me.p_Feed = New FeedInfo
                    Me.p_Feed = FeedHandlerController.GetFeed(Me.FeedId)
                End If

                Return Me.p_Feed
            End Get
        End Property

        ''' <summary>
        ''' Rule - this property contains the FeedRule that we are currently editing
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Private Property Rule() As FeedRuleInfo
            Get
                If Me.p_Rule Is Nothing Then
                    Me.p_Rule = New FeedRuleInfo
                    Me.p_Rule = FeedHandlerController.GetFeedRule(Me.RuleId)
                End If

                Return Me.p_Rule
            End Get
            Set(ByVal value As FeedRuleInfo)
                Me.p_Rule = value
            End Set
        End Property

#End Region

#Region " Event Handlers "

        ''' <summary>
        ''' Page_Load runs when the control is loaded
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        ''' </history>
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                If Not Page.IsPostBack Then
                    Me.BindData()
                End If
            Catch exc As Exception ' Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        Private Sub lnkCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCancel.Click
            Me.GoBack()
        End Sub

        Private Sub lnkDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkDelete.Click
            If RuleId <> Null.NullInteger Then
                FeedHandlerController.DeleteFeedRule(Me.RuleId)
                Response.Redirect(NavigateURL())
            Else
                DotNetNuke.UI.Skins.Skin.AddModuleMessage(Me, _
                    Me.GetLocalizedString("Error.RuleIdNull"), _
                    Skins.Controls.ModuleMessage.ModuleMessageType.RedError)
            End If
        End Sub

        Private Sub lnkSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkSubmit.Click
            If Page.IsValid Then
                Try
                    If Me.RuleId > 0 Then
                        FeedHandlerController.UpdateFeedRule(Me.RuleId, Me.Feed.FeedId, Me.txtRuleName.Text, _
                            Me.txtLookFor.Text, Me.txtReplaceWith.Text)
                    Else
                        FeedHandlerController.AddFeedRule(Me.Feed.FeedId, Me.txtRuleName.Text, _
                            Me.txtLookFor.Text, Me.txtReplaceWith.Text)
                    End If

                    Me.GoBack()
                Catch ex As Exception
                    DotNetNuke.UI.Skins.Skin.AddModuleMessage(Me, _
                        String.Concat(ex.Message, "<br />", ex.StackTrace), _
                        Skins.Controls.ModuleMessage.ModuleMessageType.RedError)
                    DotNetNuke.Services.Exceptions.LogException(ex)
                End Try
            End If
        End Sub

        Private Sub lnkTestRule_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkTestRule.Click
            If String.IsNullOrEmpty(Me.txtLookFor.Text) Or String.IsNullOrEmpty(Me.txtReplaceWith.Text) Then
                DotNetNuke.UI.Utilities.ClientAPI.RegisterStartUpScript(Me.Page, _
                    "WnsXfhEditRule_Alert", _
                    Me.GetClientScriptBlock(String.Format("alert('{0}');", Me.GetLocalizedString("Error.FillInForm"))))
            Else
                DotNetNuke.UI.Utilities.ClientAPI.RegisterStartUpScript(Me.Page, _
                    "WnsXfhEditRule_Window", _
                    Me.GetClientScriptBlock(String.Format("window.open('{0}','WnsXfhFeedComparer','status=0,toolbar=0,menubar=0,resizable=0,scrollbars=1,height=600,width=800');", _
                        Me.GetFeedruleTestUrl)))
            End If
        End Sub

#End Region

#Region " Private Methods "

        ''' <summary>
        ''' BindData - this method initiates the binding of all controls
        ''' </summary>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Private Sub BindData()
            Me.LocalizePage()

            Me.lnkDelete.Visible = (Me.RuleId <> Null.NullInteger)
            Me.lnkTestRule.Attributes.Add("onclick", "runWnsXfhRuleTest();")

            If Me.RuleId <> Null.NullInteger Then
                If Not Me.Rule Is Nothing Then
                    Me.txtRuleName.Text = Me.Rule.RuleName
                    Me.txtLookFor.Text = Me.Rule.LookFor
                    Me.txtReplaceWith.Text = Me.Rule.ReplaceWith
                Else
                    DotNetNuke.UI.Skins.Skin.AddModuleMessage(Me, Me.GetLocalizedString("Error.RuleNotFound"), _
                        Skins.Controls.ModuleMessage.ModuleMessageType.RedError)
                End If
            End If
        End Sub

        ''' <summary>
        ''' LocalizePage - this method centralizes all localization for the controls
        ''' </summary>
        ''' <remarks></remarks>
        ''' <history>
        ''' [wstrohl] - 20090324 - Added the validation controls
        ''' </history>
        Private Sub LocalizePage()
            Me.lnkCancel.Text = Me.GetLocalizedString("lnkCancel.Text")
            Me.lnkDelete.Text = Me.GetLocalizedString("lnkDelete.Text")
            Me.lnkDelete.Attributes.Add("onclick", String.Format("return confirm('{0}');", Me.GetLocalizedString("lnkDelete.Confirm")))
            Me.lnkSubmit.Text = Me.GetLocalizedString("lnkSubmit.Text")
            Me.lnkTestRule.Text = Me.GetLocalizedString("lnkTestRule.Text")

            Me.rfvLookFor.ErrorMessage = Me.GetLocalizedString("rfvLookFor.ErrorMessage")
            Me.rfvRuleName.ErrorMessage = Me.GetLocalizedString("rfvRuleName.ErrorMessage")
        End Sub

        ''' <summary>
        ''' GoBack - this method saves a line of repeating code, as the goback gets called in more than one place
        ''' </summary>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Private Sub GoBack()
            ' go back to the feed rule listing
            Response.Redirect(EditUrl(Null.NullString, Null.NullString, "EditRules", String.Concat("FeedId=", Me.Feed.FeedId)))
        End Sub

        ''' <summary>
        ''' GetFeedRuleTestUrl - this method returns the URL used to test the regex, including querystring key/value pairs
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Private Function GetFeedRuleTestUrl() As String
            Try
                Dim strUrl As String = Page.ResolveClientUrl(String.Concat(Me.ModulePath, "FeedRuleTest.aspx"))
                Dim strQsFormat As String = "?FeedUrl={0}&LookFor={1}&ReplaceWith={2}"
                If Me.Feed.FeedModuleId > Null.NullInteger Then
                    strQsFormat = String.Format(strQsFormat, _
                        Server.UrlEncode(FeedHandlerController.GetDNNRssUrl(Me.Feed.FeedModuleId)), _
                        Server.UrlEncode(Me.txtLookFor.Text), Server.UrlEncode(Me.txtReplaceWith.Text))
                Else
                    strQsFormat = String.Format(strQsFormat, _
                        Server.UrlEncode(Me.Feed.FeedImportUrl), _
                        Server.UrlEncode(Me.txtLookFor.Text), Server.UrlEncode(Me.txtReplaceWith.Text))
                End If
                Return String.Concat(strUrl, strQsFormat)
            Catch ex As Exception
                DotNetNuke.Services.Exceptions.LogException(ex)
                Throw ex
            End Try
        End Function

#End Region

    End Class

End Namespace
